//
//  MyViewController.m
//  OurMoon
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "MyViewController.h"
#import "MyTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "MyStoreViewController.h"
#import "MyNightTableViewCell.h"
#import "NightHeader.h"
#import "MyCleanTableViewCell.h"
#import "CleanCache.h"
#import "MyAdviceViewController.h"
#import "NightHeader.h"
#import "MyAboutUsViewController.h"
@interface MyViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *array;
@property(nonatomic, strong)NSArray *imageArray;
@property(nonatomic, strong)UIAlertController *alertController;
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UITableView alloc]init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView NightWithType:UIViewColorTypeNormal];
    
    [self.view addSubview:_tableView];
    
    self.array = [NSArray arrayWithObjects:@"我的收藏", @"清理缓存", @"夜间模式", @"关于我们", nil];
    self.imageArray = [NSArray arrayWithObjects:@"iconfont-heart (1)", @"iconfont-lajiqinglix",@"iconfont-yueliang",@"iconfont-ziti", @"iconfont-iconfontyijianfankui", @"iconfont-guanyuwomenicon", nil];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        static NSString *identifier = @"cell";
        MyNightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[MyNightTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = _array[indexPath.row];
        [cell.textLabel NightWithType:UIViewColorTypeNormal];
        cell.imageView.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.rightSwitch addTarget:self action:@selector(nightAction) forControlEvents:UIControlEventTouchUpInside];
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }
    else if (indexPath.row == 1)
    {
        static NSString *identifier = @"cell";
        MyCleanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[MyCleanTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = _array[indexPath.row];
        [cell.textLabel NightWithType:UIViewColorTypeNormal];
        cell.imageView.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
        cell.rightLabel.text = [NSString stringWithFormat:@"%.2fM", [CleanCache folderSizeAtPath:[CleanCache getCachesPath:@"Caches"]]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }
    else
    {
        static NSString *identifier = @"cell";
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
        cell.textLabel.text = _array[indexPath.row];
        [cell.textLabel NightWithType:UIViewColorTypeNormal];
        cell.imageView.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MyStoreViewController *myStoreVC = [[MyStoreViewController alloc]init];
        [self.navigationController pushViewController:myStoreVC animated:YES];
    }
    else if (indexPath.row == 1)
    {
        self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定清除缓存吗?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            MyCleanTableViewCell *cleanTableViewCell = (MyCleanTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
            [CleanCache cleanCaches:[CleanCache getCachesPath:@"Caches"]];
            cleanTableViewCell.rightLabel.text = [NSString stringWithFormat:@"%.2fM", [CleanCache folderSizeAtPath:[CleanCache getCachesPath:@"Caches"]]];
        }];
        [_alertController addAction:action1];
        [_alertController addAction:action2];
        [self presentViewController:_alertController animated:YES completion:nil];
    }
    else if (indexPath.row == 3)
    {
        MyAboutUsViewController *aboutUS = [[MyAboutUsViewController alloc]init];
        aboutUS.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:aboutUS animated:YES];
    }
}

-(void)nightAction
{
    [ThemeManage shareThemeManage].isNight = ![ThemeManage shareThemeManage].isNight;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil];
    [[NSUserDefaults standardUserDefaults] setBool:[ThemeManage shareThemeManage].isNight forKey:@"night"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
